// Package rank @program: gateway
//@author: edte
//@create: 2021-12-29 18:18
package rank

import (
	"context"
	"gateway/client"
	"gateway/tools/security"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"time"

	leaderboard "gateway/config/rankinglist"
	"google.golang.org/grpc"
)

var conn, err = grpc.Dial(*client.RankAddr, grpc.WithInsecure())
var c = leaderboard.NewProdScoreBoardServiceClient(conn)

// GetRankingBoard /*func GetLink() (leaderboard.ProdScoreBoardServiceClient, error) {
func GetRankingBoard(token string, boardName string, startPage int, endPage int) (gin.H, error) {
	if err != nil {
		log.Println(err.Error())
	}
	userinfo, err := security.VerifyAction(token)
	if err != nil {
		return gin.H{"status": http.StatusBadGateway, "message": err.Error()}, err
	}
	_, ok := security.UserMap[userinfo.UserID]
	if !ok {
		return gin.H{"status": http.StatusBadGateway, "message": "账号未登录"}, err
	}
	/*c, err := GetLink()
	if err != nil {
		return gin.H{"status": http.StatusBadRequest}, err
	}*/
	cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()
	request, err := c.GetUserForm(cxt, &leaderboard.ProdScoreBoardRequest{BoardName: boardName, StartPage: int32(startPage), EndPage: int32(endPage)})
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": err.Error()}, err
	}
	return gin.H{"status": request.Status, "message": "查询成功", "data": request.Ranking}, err
}
